package com.yvtong.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.yvtong.Service.YonghuManager;
import com.yvtong.entity.yonghu;

/**
 * Servlet Filter implementation class HoutaiFilter
 */
@WebFilter(filterName = "loginFilter",
urlPatterns = "/src/views/index.html")

public class HoutaiFilter implements Filter {

    /**
     * Default constructor. 
     */
    public HoutaiFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request2 = (HttpServletRequest) request;
        HttpServletResponse response2 = (HttpServletResponse) response;
        HttpSession session = request2.getSession();
        String xm=(String) session.getAttribute("zhanghao");
        String mm=(String) session.getAttribute("mima");
        YonghuManager u = new YonghuManager();
		yonghu y = u.chaxun(xm);
        if(y!=null&&y.getMima().equals(mm)) {
    		chain.doFilter(request2, response2);

        }else {
        	System.out.println("lanjie");
			response2.sendRedirect("/yvtong/src/views/user/login.html");

		}
		
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
